﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Turntable
{

    /// <summary>
    /// Random 帮助类,支持多线调用
    /// </summary>
    public static class RandomHelper
    {
        private static readonly Random _global = new Random();
        [ThreadStatic] private static Random _local;


        /// <summary>
        /// 获取随机整数
        /// </summary>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <returns>随机数 大于等于 minValue，小于 maxValue</returns>
        public static int RandomIntValue(int minValue, int maxValue)
        {
            if (_local == null)
            {
                lock (_global)
                {
                    if (_local == null)
                    {
                        int seed = _global.Next();
                        _local = new Random(seed);
                    }
                }
            }
            return _local.Next(minValue, maxValue);
        }

        /// <summary>
        /// 获取随机整数
        /// </summary>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <returns>随机数 大于等于 minValue，小于 maxValue</returns>
        public static int RandomIntValue1(int minValue, int maxValue)
        {
            if (_local == null)
            {
                lock (_global)
                {
                    if (_local == null)
                    {
                        _local = new Random(GetRandomSeedbyGuid());
                    }
                }
            }
            return _local.Next(minValue, maxValue);
        }

        /// <summary>
        /// 获取随机整数 
        /// </summary>
        /// <param name="maxValue"></param>
        /// <returns>随机数 大于等于0 且小于 maxValue</returns>
        public static int RandomIntValue(int maxValue)
        {
            if (_local == null)
            {
                lock (_global)
                {
                    if (_local == null)
                    {
                        int seed = _global.Next();
                        _local = new Random(seed);
                    }
                }
            }
            return _local.Next(maxValue);
        }

        /// <summary>
        /// 乱序排序 List 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<T> Shuffle<T>(List<T> list)
        {
            for (int i = 0; i < list.Count; ++i)
            {
                int j = RandomIntValue(i, list.Count);
                T temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }
            return list;
        }


        /// <summary>
        /// 使用Guid生成种子
        /// </summary>
        /// <returns></returns>
        public static int GetRandomSeedbyGuid()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            int iSeed = BitConverter.ToInt32(buffer, 0);
            return iSeed;

        }

    }
}
